﻿/*-----------------------------------------------
// File: ResourceLoadState.cs 
// Description: 渲染层加载资源状态
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using AssetLoad;
using System.Collections.Generic;

namespace Battle.View
{
    public class ResourceLoadState : Fsm.FsmStateMonoBase<BattleState>
    {
        BattleViewCtrl viewCtrl = null;
        public ResourceLoadState(BattleState state, BattleViewCtrl ctrl) : base(state, ctrl)
        {
            viewCtrl = ctrl;
        }
        private float beginTimer;

        public override void Enter()
        {
            BattleManager.Instance.battlePanel.Hide();
            beginTimer = Time.timeSinceLevelLoad;
            viewCtrl.StartCoroutine(LoadResource());
        }

        protected virtual IEnumerator LoadResource()
        {
            var a = System.DateTime.Now;
            string sceneName = AssetBundleManager.Instance.GetReplaceSceneName(StaticData.curBattleData.mapSceneName);
            if (!viewCtrl.IsSceneLoaded)
            {
                if (StaticData.curBattleData.battleType == BattleType.PVE)
                {
                    List<string> unloadScenes = new List<string>();
                    for (int i = 0; i < SceneManager.sceneCount; ++i)
                    {
                        var s = SceneManager.GetSceneAt(i);
                        if (s.name != sceneName)
                        {
                            if (s.name.IndexOf("run_") == -1)
                            {
                                unloadScenes.Add(s.name);
                            }
                            else
                            {
                                var gos = s.GetRootGameObjects();
                                foreach (var g in gos)
                                {
                                    g.SetActive(false);
                                }
                            }
                        }
                        //else
                        //{
                        //    SceneManager.SetActiveScene(s);
                        //    Debug.Log("Set active scene " + s.name);
                        //    var gos = s.GetRootGameObjects();
                        //    foreach (var g in gos)
                        //    {
                        //        g.SetActive(true);
                        //    }
                        //    if (LightmapSettings.lightmaps[0].lightmapFar != orginLm[0].lightmapFar)
                        //    {
                        //        List<LightmapData> lds2 = new List<LightmapData>();
                        //        for (int j = 0; i < LightmapSettings.lightmaps.Length; ++j)
                        //        {
                        //            LightmapData ld = new LightmapData();
                        //            ld.lightmapFar = LightmapSettings.lightmaps[j].lightmapFar;
                        //            ld.lightmapNear = null;
                        //            lds2.Add(ld);
                        //        }
                        //        List<LightmapData> lds3 = new List<LightmapData>(orginLm);
                        //        lds3.AddRange(lds2);
                        //        LightmapSettings.lightmaps = lds3.ToArray();
                        //        Debug.Log("change lightmaps " + lds3.Count);
                        //    }
                        //}
                    }
                    foreach( var s in unloadScenes)
                    {
                        SceneManager.UnloadScene(s);
                    }
                    unloadScenes.Clear();

                    List<LightmapData> orginLm = new List<LightmapData>();
                    for (int i = 0; i < LightmapSettings.lightmaps.Length; ++i)
                    {
                        LightmapData ld = new LightmapData();
                        ld.lightmapFar = LightmapSettings.lightmaps[i].lightmapFar;
                        ld.lightmapNear = null;
                        orginLm.Add(ld);
                    }

                    var battleScene = SceneManager.GetSceneByName(sceneName);
                    if (!battleScene.isLoaded)
                    {
                        yield return AssetBundleManager.Instance.LoadSceneAsync(SceneName.sceneFolder + sceneName,
                            sceneName, true);
                        yield return 1;
                        battleScene = SceneManager.GetSceneByName(sceneName);
                    }
                    SceneManager.SetActiveScene(battleScene);
                    //else
                    //{
                    Debug.Log("Set active scene " + battleScene.name);
                    var bgos = battleScene.GetRootGameObjects();
                    foreach (var g in bgos)
                    {
                        g.SetActive(true);
                    }
                    if (LightmapSettings.lightmaps[0].lightmapFar != orginLm[0].lightmapFar)
                    {
                        List<LightmapData> lds2 = new List<LightmapData>();
                        for (int j = 0; j < LightmapSettings.lightmaps.Length; ++j)
                        {
                            LightmapData ld = new LightmapData();
                            ld.lightmapFar = LightmapSettings.lightmaps[j].lightmapFar;
                            ld.lightmapNear = null;
                            lds2.Add(ld);
                        }
                        List<LightmapData> lds3 = new List<LightmapData>(orginLm);
                        lds3.AddRange(lds2);
                        LightmapSettings.lightmaps = lds3.ToArray();
                        Debug.Log("change lightmaps " + lds3.Count);
                    }
                    //}

                    yield return 1;
                }
                else
                {
                    yield return AssetBundleManager.Instance.LoadSceneAsync(SceneName.sceneFolder + sceneName,
                        sceneName);
                    yield return 1;
                }
                viewCtrl.IsSceneLoaded = true;
                
            }
            Debug.Log("load scene " + sceneName + " cost time: " + (System.DateTime.Now - a).TotalSeconds);
            //a = System.DateTime.Now;

            CameraTool.Instance.ResetSceneBloomEffect();
            viewCtrl.StartCoroutine(viewCtrl.LoadBase());
            viewCtrl.BuildActorFromCache();
            foreach (var d in viewCtrl.GetAllActors())
            {
                d.Value.LoadResourceAsync();
            }

        }

        protected virtual bool LoadComplete()
        {
            if (!viewCtrl.IsSceneLoaded)
                return false;
            if (!viewCtrl.IsBattleBaseLoaded)
                return false;
            foreach (var d in viewCtrl.GetAllActors())
                if (!d.Value.LoadComplete())
                    return false;
            Debug.Log("load resource complete ");
            return true;
        }

        public override void Update()
        {
            if (LoadComplete())
            {
                ToEntranceState();
                return;
            }
        }

        public void ToEntranceState()
        {
            viewCtrl.SwitchState(BattleState.EntranceState);
        }


        public override void Leave()
        {
            Debug.LogWarning("Resource load cost time: " + (Time.timeSinceLevelLoad - beginTimer)+"s");
        }



    }
}